import { useRef, forwardRef, useImperativeHandle } from 'react';

interface RefHandle {
  a: number;
  b: () => void;
}
interface IProps {

}

const Son = forwardRef<RefHandle, IProps>((_props, ref) => {
  useImperativeHandle(ref, () => ({
    a: 1,
    b: () => console.log('方法被执行了')
  }));
  return '子组件';
});


const Father = () => {
  const sonRef = useRef<RefHandle>(null);
  return (
    <>
      '父组件'
      <button onClick={() => {
        console.log(sonRef.current);
        console.log(sonRef.current!.a);
        sonRef.current!.b();
      }}>获取子组件</button>
      <Son ref={sonRef} />
    </>
  );
}
export default Father;
